/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditProduct.java
 *
 * Created on 19-dec-2011, 12:44:17
 */
package view.manage.inventory;

import java.util.List;
import model.Category;
import javax.swing.JOptionPane;
import main.WinkelApplication;
import model.Product;

/**
 *
 * @author Robbie
 */
public class EditProduct extends javax.swing.JPanel {

    List<Category> categories;
    Product product;
    
        /** Creates new form EditProduct */
        public EditProduct(int product_id) {

        initComponents();
        getAndFillCategories();
        product = WinkelApplication.getQueryManager().getProduct(product_id);
        
        // Put text from table in edittable textfields
        
        fetchAndFillCategory();
        jTextName.setText(product.getName() + "");
        jTextBeschrijving.setText(product.getDescription() + "");
        jTextPrijs.setText(product.getPrice() + "");
        jTextVoorraad.setText(product.getStock() + "");
        //jLabel7.setText(product.getProductId() + "");

    }
    
    private void getAndFillCategories(){
        //retrieve all existing categories from the DB
        categories = WinkelApplication.getQueryManager().getCategories();
        //add every category to the dropdown box
        for(Category category : categories){
            cbCategory.addItem(category.getName());
        }
    }
    
    private int getSelectedCategoryID(){
        String selectedCategoryName = (String) cbCategory.getSelectedItem();
        int selectedCategoryID = 0;
        for(Category category : categories){
            if (selectedCategoryName.equalsIgnoreCase(category.getName())){
                selectedCategoryID = category.getCategoryId();
                break;//the ID has been found, the for loop can stop now
            }
        }
        return selectedCategoryID;
    }
    
    void fetchAndFillCategory(){
        int categoryID = product.getCategorieId();
        for(Category category : categories){
            if (categoryID == category.getCategoryId()){
                cbCategory.setSelectedItem(category.getName());
                break;//the ID has been found, the for loop can stop now
            }
        }
    }
        
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel6 = new javax.swing.JLabel();
        jTextBeschrijving = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextPrijs = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextVoorraad = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButtonBack = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jButtonAdd = new javax.swing.JButton();
        cbCategory = new javax.swing.JComboBox();
        lbTitle = new javax.swing.JLabel();

        jLabel6.setText("Product ID =");

        setMinimumSize(new java.awt.Dimension(650, 479));
        setPreferredSize(new java.awt.Dimension(838, 479));
        setLayout(new java.awt.GridBagLayout());

        jTextBeschrijving.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextBeschrijvingActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.ipadx = 135;
        gridBagConstraints.ipady = 52;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 54, 0, 0);
        add(jTextBeschrijving, gridBagConstraints);

        jLabel5.setText("Beschrijving");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(20, 54, 0, 0);
        add(jLabel5, gridBagConstraints);

        jLabel4.setText("Product Naam");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.ipadx = 19;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(26, 47, 0, 0);
        add(jLabel4, gridBagConstraints);

        jTextName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextNameActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.ipadx = 80;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(1, 47, 0, 0);
        add(jTextName, gridBagConstraints);

        jLabel3.setText("Prijs");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 31;
        gridBagConstraints.ipady = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 47, 0, 0);
        add(jLabel3, gridBagConstraints);

        jTextPrijs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextPrijsActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.ipadx = 80;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(1, 47, 0, 0);
        add(jTextPrijs, gridBagConstraints);

        jLabel2.setText("Voorraad");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 47, 0, 0);
        add(jLabel2, gridBagConstraints);

        jTextVoorraad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextVoorraadActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.ipadx = 80;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 47, 0, 0);
        add(jTextVoorraad, gridBagConstraints);

        jLabel1.setText("Category ID");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 47, 0, 0);
        add(jLabel1, gridBagConstraints);

        jButtonBack.setText("Terug");
        jButtonBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBackActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 30;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 74, 283, 150);
        add(jButtonBack, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(jLabel9, gridBagConstraints);

        jButtonAdd.setText("Pas aan");
        jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 66;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 54, 283, 0);
        add(jButtonAdd, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 47, 0, 0);
        add(cbCategory, gridBagConstraints);

        lbTitle.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lbTitle.setText("Product aanpassen");
        add(lbTitle, new java.awt.GridBagConstraints());
    }// </editor-fold>//GEN-END:initComponents

private void jTextBeschrijvingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextBeschrijvingActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextBeschrijvingActionPerformed

private void jTextNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextNameActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextNameActionPerformed

private void jTextPrijsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextPrijsActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextPrijsActionPerformed

private void jTextVoorraadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextVoorraadActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextVoorraadActionPerformed

private void jButtonBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBackActionPerformed

    //Back to the catalog
    WinkelApplication.getInstance().showPanel(new view.manage.inventory.Catalog());

        
}//GEN-LAST:event_jButtonBackActionPerformed

private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
      
        // Make new product with editted values
        Product editproduct = new Product();
        
        editproduct.setProductId(product.getProductId());
        editproduct.setCategorieId(getSelectedCategoryID());
        editproduct.setName(jTextName.getText());
        editproduct.setPrice(Double.parseDouble(jTextPrijs.getText()));
        editproduct.setDescription(jTextBeschrijving.getText());
        editproduct.setStock(Integer.parseInt(jTextVoorraad.getText()));
        
        // Change the values in the database to the editted values
        WinkelApplication.getQueryManager().updateProduct(editproduct);
        System.out.println("Product edited");
        WinkelApplication.getInstance().showPanel(new view.manage.inventory.Catalog());

        // Terug naar leeg scherm
}//GEN-LAST:event_jButtonAddActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbCategory;
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonBack;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jTextBeschrijving;
    private javax.swing.JTextField jTextName;
    private javax.swing.JTextField jTextPrijs;
    private javax.swing.JTextField jTextVoorraad;
    private javax.swing.JLabel lbTitle;
    // End of variables declaration//GEN-END:variables
}
